<!--评论模块-->
<template>
<div>
  <div class="container">     
    <div class="comment" v-for="(item,index) in comments" :key="index">           
      <div class="info">
        <img class="avatar" :src="item.visitUser.vImage" width="36" height="36"/>
        <div class="right">
          <div class="name">{{item.visitUser.vUsername}}</div>
          <div class="date">{{item.mCommitTime}}</div>
        </div>
      </div>
      <div class="content">{{item.mContent}}</div>
      <div class="control">        
        <span class="comment-reply" @click="showCommentInput(item)">
          <!-- 评论图标 -->
          <font-awesome-icon :icon="['far', 'comment']" />
          <span>&nbsp;回复</span>
        </span>
        <!--:class="{active: item.isLike}"修改-->
        <span class="like"  @click="likeClick(item)">
          <!-- 赞图标 -->
          <font-awesome-icon :icon="['far', 'thumbs-up']" />
          <span class="like-num">&nbsp;{{item.mPraiseNum}}</span>
          <!-- <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span> -->
        </span>
      </div>
      <div class="reply" v-if="item.commentReplayList">
        <div class="item" v-for="(reply,index) in item.commentReplayList" :key="index">
          <div class="reply-content">
             <img class="avatar" :src="reply.visitUserImage" width="36" height="36"/>
            <span class="from-name">{{reply.visitUserUsername}}</span><span>: </span>
            <span class="to-name">@{{reply.parentUsername}}</span>
            <span>{{reply.rContent}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.rCommitTime}}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <font-awesome-icon :icon="['far', 'comment']" />
              <span>&nbsp;回复&nbsp;&nbsp;</span>              
            </span>
            <span class="praise" @click="likeClick(item)">
              <font-awesome-icon :icon="['far', 'thumbs-up']" />
              <span class="like-num">{{reply.rPraiseNum}}</span>
            </span>
          </div>
        </div>
        
        <div class="write-reply">                         
          <input-component :show="showItemId === item.mCommentId"
                          :value="inputComment"
                          :replyTo="replyTo"
                          @cancel="cancelInput"
                          @confirmReplay="commitReplayComment">
          </input-component> 
        </div>       
      </div>
    </div>
  </div>

</div>

</template>

<script>

  import Vue from 'vue'
  import commentInput from './comment_input'
  import * as getIp from "../../../static/front-vue/js/getip.js"
  import {getTime} from "../../../static/front-vue/js/time.js"

  export default {
    props: {
       comments: {
         type: Array,
         required: true
       }     
       
    },
    components: {
      "input-component": commentInput
    },
    data() {
      return {
        //showInput:false,
        inputComment: '',
        showItemId: '',           
        replyTo:"",  //被回复者名字
        /**
         * 非主评论对象
         */
        replyCommitObj:{
          rContent:"",
          mUsername:"",
          mPwd:"",
          mImageUrl:"",
          rCommentType:"",
          mCommentId:"",
          parentId:"",
          rCommitTime:"",
          ip:"",
          area:"",
          newUserPwd:"",
          userType:""
        },   //需要提交的所有信息(这里全部是回复)
        
        ipInfo:{
          ip:"",
          area:"",
          brower:"",
          os:""
        }
      }
    },
    computed: {},
    watch: {
       
    },
    methods: {
      /**
       * 点赞
       */
      likeClick(item) {
        if (item.isLike === null) {
          Vue.$set(item, "isLike", true);
          item.likeNum++
        } else {
          if (item.isLike) {
            item.likeNum--
          } else {
            item.likeNum++
          }
          item.isLike = !item.isLike;
        }
      },

      /**
       * 点击取消按钮
       */
      cancelInput() {
        this.showItemId = ''
      },      

      /**
       * 提交回复类型评论
       */
      commitReplayComment(visitUser,value) {
        
        //value是获取到的子组件的评论内容,将信息传到后台
        this.replayCommitObj.rContent = value;
        this.replayCommitObj.rCommitTime = getTime(); //获得当前时间

        //提交评论者的ip信息
        this.replayCommitObj.ip = this.ipInfo.ip;
        this.replayCommitObj.area = this.ipInfo.area;

        this.mainCommentObj.mUsername = visitUser.username;
        this.mainCommentObj.mPwd = visitUser.pwd;    
        this.mainCommentObj.newUserPwd = visitUser.setPwd;
        this.mainCommentObj.mImageUrl = visitUser.imageUrl;
        this.mainCommentObj.userType = visitUser.userType;
        
        this.commitByAxios(this.replyCommitObj,"/comment/commitReplayComment");      
           
      },

      /**
       * axios提交操作
       */
      commitByAxios(commentInfo,commitUrl) {
        var _this = this; 

        console.log(data);
        _this.$axios({
          method:"post",
          url:commitUrl,
          data:{
            comment:commentInfo
          }}).then((res)=>{
            if (resp && resp.status === 200) 
            {                    
              //接收提交执行的结果
              if(resp.data == "success") {

                alert("提交成功");
                //触发父组件的方法，重新加载评论数据
                this.$emit("loadComments");

                //隐藏输入框
                this.cancelInput();
              }               
              else
                alert("提交失败");                                               
            }
        });
      },

      /**
       * 点击评论按钮显示输入框
       * item: 当前大评论
       * reply: 当前回复的评论
       * 并且此处还要设置评论有关父评论的信息
       */
      showCommentInput(item, reply) {
        //评论类型
        var commentType = "";
        this.replayCommitObj = {};

        //回复评论
        if (reply) {
          this.inputComment = "@" + reply.vUserName + " ";
          this.replyTo = reply.visitUserUsername;   //设置replyTo名称，分为根评论和回复

          commentType = 2;
          this.replayCommitObj.parentId = reply.rCommentId;
        } else {  //主评论
          this.inputComment = '';
          this.replyTo = item.visitUser.vUsername;

          commentType = 1;
          this.replayCommitObj.parentId = item.mCommentId;
        }
        
        this.replayCommitObj.rCommentType = commentType;  //类型
        this.replayCommitObj.mCommentId = item.mCommentId;  //根评论id

        this.showItemId = item.mCommentId
      },
      
    },
    
    created() {
      //创建之后获取ip和地址
      this.ipInfo.ip = sessionStorage.getItem("ip");
      this.ipInfo.area = sessionStorage.getItem("area");
      this.ipInfo.brower = getIp.GetCurrentBrowser();
      this.ipInfo.os = getIp.GetOs();  
    }
  }
</script>

<style scoped lang="scss">

  @import "../../../static/front-vue/scss/index";

  .container {
    padding: 0 10px;
    box-sizing: border-box;
    border:0px solid #C0C4CC;
    border-radius:30px;
    margin-bottom: 10px;
    font-weight: 400;
    .comment {
      display: flex;
      flex-direction: column;
      padding: 10px;
      border-bottom: 1px solid $border-fourth;
      .info {
        display: flex;
        align-items: center;
        .avatar {
          border-radius: 50%;
        }
        .right {
          display: flex;
          flex-direction: column;
          margin-left: 10px;
          .name {
            font-size: 16px;
            color: $text-blueviolet;
            margin-bottom: 5px;
            font-weight: 500;
          }
          .date {
            font-size: 12px;
            color: $text-minor;
          }
        }
      }
      .content {
        font-size: 15px;
        color: $text-main;
        line-height: 20px;
        padding: 10px 0;
      }
      .control {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;
        .like {
          display: flex;
          align-items: center;
          margin-left: 20px;
          cursor: pointer;
          &.active, &:hover {
            color: $color-warning;
          }
          .iconfont {
            font-size: 14px;
            margin-right: 5px;
          }
        }
        .comment-reply {
          display: flex;
          align-items: center;
          cursor: pointer;
          &:hover {
            color: $color-warning;
          }
          .iconfont {
            font-size: 16px;
            margin-right: 5px;
          }
        }

      }
      .reply {
        margin: 10px 0;
        border-left: 2px solid $border-first;
        .item {
          margin: 0 10px;
          padding: 10px 0;
          border-bottom: 1px dashed $border-third;
          .reply-content {
            display: flex;
            align-items: center;
            font-size: 14px;
            color: $text-main;
            .from-name {
              color: $color-main;
            }
            .to-name {
              color: $color-main;
              margin-left: 5px;
              margin-right: 5px;
            }
          }
          .reply-bottom {
            display: flex;
            align-items: center;
            margin-top: 6px;
            font-size: 12px;
            color: $text-minor;            
            .reply-text {
              display: flex;
              align-items: center;
              margin-left: 10px;
              cursor: pointer;
              &:hover {
                color: $color-warning;
              }
              .icon-comment {
                margin-right: 5px;
              }
            }
            .praise {
              margin-left: 10px;
              cursor: pointer;
              &:hover {
                color: $color-warning;
              }
            }
          }
        }
        .write-reply {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-minor;
          padding: 10px;              
          .el-icon-edit {
            margin-right: 5px;
          }
        }
        .fade-enter-active, fade-leave-active {
          transition: opacity 0.5s;
        }
        .fade-enter, .fade-leave-to {
          opacity: 0;
        }
        .input-wrapper {
          padding: 10px;
          .gray-bg-input, .el-input__inner {
            /*background-color: #67C23A;*/
          }
          .btn-control {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            padding-top: 10px;
            .cancel {
              font-size: 16px;
              color: $text-normal;
              margin-right: 20px;
              cursor: pointer;
              &:hover {
                color: $text-333;
              }
            }
            .confirm {
              font-size: 16px;
            }
          }
        }
      }
    }
  }
</style>
